<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="登录人员" prop="userCode">
          <el-input
            v-model="queryParams.userCode"
            placeholder="登录人员"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item label="登录状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="登录状态"
            clearable
            class="!w-[100px]"
          >
            <el-option label="成功" value="1" />
            <el-option label="失败" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
<!--        <el-button-->
<!--          v-hasPerm="['sys_login_log_add']"-->
<!--          type="success"-->
<!--          @click="handleOpenDialog()"-->
<!--        >-->
<!--          <template #icon><Plus /></template>-->
<!--          新增-->
<!--        </el-button>-->
        <el-button
          type="danger"
          :disabled="removeIds.length === 0"
          @click="handleDelete()"
        >
          <template #icon><Delete /></template>
          删除
        </el-button>
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          key="userCode"
          label="登录人员"
          prop="userCode"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="ipaddr"
          label="登录IP地址"
          prop="ipaddr"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="loginLocation"
          label="登录地点"
          prop="loginLocation"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="browser"
          label="浏览器类型"
          prop="browser"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="os"
          label="操作系统"
          prop="os"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="msg"
          label="提示消息"
          prop="msg"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="status"
          label="登录状态"
          prop="status"
          min-width="150"
          align="center"
        >
          <template #default="scope">
            <DictLabel
              code="sys_common_status"
              v-model="scope.row.status"
              size="small"/>
          </template>
        </el-table-column>
        <el-table-column
          key="loginTime"
          label="访问时间"
          prop="loginTime"
          min-width="150"
          align="center"
        />
        <el-table-column fixed="right" label="操作" width="180" align="center">
          <template #default="scope">
<!--            <el-button-->
<!--              v-hasPerm="['sys_login_log_edit']"-->
<!--              type="primary"-->
<!--              size="small"-->
<!--              link-->
<!--              @click="handleOpenDialog(scope.row.id)"-->
<!--            >-->
<!--              <template #icon><Edit /></template>-->
<!--              编辑-->
<!--            </el-button>-->
            <el-button
              v-hasPerm="['sys_log_edit']"
              type="primary"
              size="small"
              link
              @click="handleOpenDialog(scope.row.id)"
            >
              <template #icon>
                <View/>
              </template>
              查看详情
            </el-button>
            <el-button
              v-hasPerm="['sys_login_log_del']"
              type="danger"
              size="small"
              link
              @click="handleDelete(scope.row.id)"
            >
              <template #icon><Delete /></template>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.current"
        v-model:limit="queryParams.size"
        @pagination="handleQuery()"
      />
    </el-card>

    <!-- 系统访问日志表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="500px"
      @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="登录人员" prop="userCode">
          {{formData.userCode}}
        </el-form-item>
        <el-form-item label="登录IP地址" prop="ipaddr">
          {{formData.ipaddr}}
        </el-form-item>
        <el-form-item label="登录地点" prop="loginLocation">
          {{formData.loginLocation}}
        </el-form-item>
        <el-form-item label="浏览器类型" prop="browser">
          {{formData.browser}}
        </el-form-item>
        <el-form-item label="操作系统" prop="os">
          {{formData.os}}
        </el-form-item>
        <el-form-item label="提示消息" prop="msg">
          {{formData.msg}}
        </el-form-item>
        <el-form-item label="登录状态" prop="status">
          <DictLabel
            code="sys_common_status"
            v-model="formData.status"
            size="small"/>
        </el-form-item>
        <el-form-item label="访问时间" prop="loginTime">
          {{formData.loginTime}}
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "LoginLog",
  inheritAttrs: false,
});

import LoginLogAPI, { LoginLogPageVO, LoginLogForm, LoginLogPageQuery } from "@/api/monitor/login-log";

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const loading = ref(false);
const removeIds = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<LoginLogPageQuery>({
  current: 1,
  size: 10,
});

// 系统访问日志表格数据
const pageData = ref<LoginLogPageVO[]>([]);

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 系统访问日志表单数据
const formData = reactive<LoginLogForm>({});

// 系统访问日志表单校验规则
const rules = reactive({
  loginLocation: [{ required: true, message: "请输入登录地点", trigger: "blur" }],
  browser: [{ required: true, message: "请输入浏览器类型", trigger: "blur" }],
  os: [{ required: true, message: "请输入操作系统", trigger: "blur" }],
  msg: [{ required: true, message: "请输入提示消息", trigger: "blur" }],
  status: [{ required: true, message: "请输入登录状态（0成功 1失败）", trigger: "blur" }],
  loginTime: [{ required: true, message: "请输入访问时间", trigger: "blur" }],
  tenantId: [{ required: true, message: "请输入租户ID", trigger: "blur" }],
  updatedTime: [{ required: true, message: "请输入修改时间", trigger: "blur" }],
  updatedUser: [{ required: true, message: "请输入修改人的id", trigger: "blur" }],
  createdTime: [{ required: true, message: "请输入创建时间", trigger: "blur" }],
  createdUser: [{ required: true, message: "请输入创建人的id", trigger: "blur" }],
});

/** 查询系统访问日志 */
function handleQuery() {
  loading.value = true;
  LoginLogAPI.getPage(queryParams)
    .then((data) => {
      pageData.value = data.records;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置系统访问日志查询 */
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.current = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 打开系统访问日志弹窗 */
function handleOpenDialog(id?: number) {
  dialog.visible = true;
  if (id) {
    dialog.title = "修改系统访问日志";
    LoginLogAPI.getFormData(id).then((data) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增系统访问日志";
  }
}

/** 提交系统访问日志表单 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        LoginLogAPI.update(formData)
          .then(() => {
            ElMessage.success("修改成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        LoginLogAPI.add(formData)
          .then(() => {
            ElMessage.success("新增成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭系统访问日志弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}

/** 删除系统访问日志 */
function handleDelete(id?: number) {
  const ids = [id || removeIds.value].join(",");
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      LoginLogAPI.deleteByIds(ids)
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    () => {
      ElMessage.info("已取消删除");
    }
  );
}

onMounted (() => {
  handleQuery();
});
</script>

